package net.trevize.datasetexplorer.dialogs;

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.regex.Pattern;

import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.MatteBorder;

import net.trevize.FSExplorer.FSTree.FSTree;
import net.trevize.datasetexplorer.DatasetExplorer;
import net.trevize.datasetexplorer.DatasetExplorerProperties;
import net.trevize.datasetexplorer.GListenerImpl;
import net.trevize.datasetexplorer.GalateePopupMenuInitializer;
import net.trevize.galatee.Galatee;
import net.trevize.galatee.GalateeFactory;
import net.trevize.gui.FSExplorer.FSE.FSEEvent;
import net.trevize.gui.FSExplorer.FSE.FSEListener;
import net.trevize.gui.layout.CellStyle;
import net.trevize.gui.layout.XGridBag;

/**
 * 
 * 
 * @author Nicolas James <nicolas.james@gmail.com> [[http://njames.trevize.net]]
 * LDDTab1.java - May 17, 2010
 */

public class LDDTab1 extends JPanel implements ActionListener, FSEListener {

	public static final String ACTION_COMMAND_CHECKBOX_RELATIVE = "ACTION_COMMAND_CHECKBOX_RELATIVE";

	private DatasetExplorer dataset_explorer;

	private FSTree fstree;
	private JCheckBox checkbox_relative_path;
	private JComboBox combo_prefix;

	public LDDTab1(DatasetExplorer dataset_explorer) {
		this.dataset_explorer = dataset_explorer;
		init();
	}

	private void init() {
		setLayout(new GridBagLayout());
		XGridBag xgb = new XGridBag(this);

		JLabel header = new JLabel();
		header.setOpaque(true);
		header.setBackground(Color.WHITE);
		header.setBorder(new MatteBorder(0, 15, 0, 0, Color.BLUE));
		header
				.setText("<html><body><h1>Load local dataset from file</h1><p>Select a local file that contains local paths to images."
						+ "<br/>"
						+ "The file is line-formatted. For each line: one image path followed by textual annotations, all fields are coma-separated."
						+ "<br/>"
						+ "Paths can be absolute or relative if you specify a prefix.</p></body></html>");
		CellStyle style0 = new CellStyle(1., 0., GridBagConstraints.NORTHWEST,
				GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0);
		xgb.add(header, style0, 0, 0, 1, 2);

		String file_filter = "(^[^\\.]$|^[^\\.].*[^~]$)";
		String dir_filter = "^[^\\.].*$";
		fstree = new FSTree(File.listRoots()[0], Pattern.compile(file_filter),
				Pattern.compile(dir_filter), true);
		fstree.addFSEListener(this);
		CellStyle style1 = new CellStyle(1., 1., GridBagConstraints.NORTHWEST,
				GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0);
		xgb.add(fstree, style1, 1, 0, 1, 2);

		checkbox_relative_path = new JCheckBox("relative paths in the file");
		checkbox_relative_path.setSelected(false);
		checkbox_relative_path.addActionListener(this);
		checkbox_relative_path
				.setActionCommand(ACTION_COMMAND_CHECKBOX_RELATIVE);
		CellStyle style2 = new CellStyle(0., 0., GridBagConstraints.WEST,
				GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0);
		xgb.add(checkbox_relative_path, style2, 2, 0, 1, 2);

		JLabel l_tf_prefix = new JLabel("prefix: ");
		xgb.add(l_tf_prefix, style2, 3, 0);

		combo_prefix = new JComboBox();
		combo_prefix.setEnabled(false);
		combo_prefix.setEditable(true);
		combo_prefix.addItem("");
		for (String user_http_uri_prefix : DatasetExplorerProperties
				.getUserFILEURIPrefixes()) {
			combo_prefix.addItem(user_http_uri_prefix);
		}
		CellStyle style3 = new CellStyle(1., 0., GridBagConstraints.WEST,
				GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0);
		xgb.add(combo_prefix, style3, 3, 1);
	}

	/***************************************************************************
	 * implementation of ActionListener
	 **************************************************************************/

	@Override
	public void actionPerformed(ActionEvent e) {
		String action_command = e.getActionCommand();

		if (action_command.equals(ACTION_COMMAND_CHECKBOX_RELATIVE)) {
			if (checkbox_relative_path.isSelected()) {
				combo_prefix.setEnabled(true);
			} else {
				combo_prefix.setEnabled(false);
			}

		}
	}

	/***************************************************************************
	 * implementation of FSEListener
	 **************************************************************************/

	@Override
	public void focusChanged(FSEEvent e) {
	}

	@Override
	public void open(FSEEvent e) {
		String prefix = null;
		if (checkbox_relative_path.isSelected()
				&& !combo_prefix.getEditor().getItem().equals("")) {
			prefix = (String) combo_prefix.getEditor().getItem();
			DatasetExplorerProperties.addUserFILEURIPrefixes(prefix);
		}

		final Galatee galatee = GalateeFactory.loadLocalDatasetFromFile(e
				.getSelectedFile().getPath(), prefix);
		galatee.addGalateeListener(new GListenerImpl());

		galatee.enableSearchFunctionality();

		GalateePopupMenuInitializer.initialize(galatee, dataset_explorer);

		dataset_explorer.addLocalDatasetExplorerTab(galatee, e.getSelectedFile()
				.getName());
		dataset_explorer.getDialog_load_dataset().setVisible(false);
	}

	@Override
	public void pathChanged(FSEEvent e) {
	}

	@Override
	public void selectionChanged(FSEEvent e) {
	}

}
